﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
namespace SQT.tracnghiem.Ajax.ASP.Events
{
    public partial class edited_event : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            int id = int.Parse(Request.QueryString["id"]);
            string name = Request.QueryString["name"];
            string player = Request.QueryString["player"];
            string games = Request.QueryString["games"];

            new TblEventController().Update(id, name, player, null);

            string[] split = games.Split(';');
            var get_plan = new TblEventPlanController().FetchAll().Where(x => x.EventId == id).ToList();
            
            if(split.Length-1 <= get_plan.Count)
            {
                for(int i=0;i<split.Length-1;i++)
                {
                    string[] split2 = split[i].Split('_');
                    int type = int.Parse(split2[0]);
                    string games_id = split2[1];
                    int time = int.Parse(split2[2]);
                    string scores = split2[3];
                    new TblEventPlanController().Update(get_plan[i].Id, null, type, games_id,time, scores);
                }
            }
            else
            {
                for (int i = 0; i < get_plan.Count - 1; i++)
                {
                    string[] split2 = split[i].Split('_');
                    int type = int.Parse(split2[0]);
                    string games_id = split2[1];
                    int time = int.Parse(split2[2]);
                    string scores = split2[3];
                    new TblEventPlanController().Update(get_plan[i].Id, null, type, games_id, time, scores);
                }
                for(int i=get_plan.Count;i<split.Length-1;i++)
                {
                    string[] split2 = split[i].Split('_');
                    int type = int.Parse(split2[0]);
                    string games_id = split2[1];
                    int time = int.Parse(split2[2]);
                    string scores = split2[3];
                    new TblEventPlanController().Insert(id, type, games_id, time, scores);
                }
            }
        }
    }
}